package org.yagnus.scala.derivatives.iterates

/**
 * This class delays the creation of an iterator until the last possible moment. The class assumes that
 * the iterable passed to it is never empty
 */

object DelayedIterator {
    class DelayedIterator[A](incomming : ⇒ Iterable[A]) extends scala.collection.Iterator[A] {

        var actualItr : Iterator[A] = null;

        def getItr : Iterator[A] = {
            if (actualItr == null) {
                actualItr = incomming.iterator;
            }
            actualItr;
        }

        def hasNext : Boolean = (actualItr == null || actualItr.hasNext);
        def next : A = actualItr.next;
        final def delayed = this;
        final def retarded = this;
    }

    implicit def retardTheIterableExecution[Q](in : Iterable[Q]) = new DelayedIterator[Q](in);
}